package com.wwwc.index.web.servlet;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.text.DecimalFormat;

import javax.servlet.ServletRequestWrapper;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.util.web.*;
import com.wwwc.index.web.ejb.database.*;

public class ScheduleDViewer extends ScheduleD {
    int lock_level =8; //allow delete all records

    //===============================================================================
    //  ScheduleD 1099 Status: 
    //  -----------------------------------------------------------------------------
    //  STATUS == 0 [Deta Loaded]
    //  STATUS == 1 [Deta Confirmed and Detail records generated]
    //  STATUS == 2 [Base year confirmed (No more transaction data before that year)]
    //  STATUS == 3 [Pre-year holding confirmed (no holding or holding added)]
    //  STATUS == 4 [Tick updated]
    //===============================================================================

    public StringBuffer main(HttpServletRequest request, String call_path, String mid,
			     String user_name, int user_level, boolean manager, 
			     boolean admin) {
	
	String aid = viewCallBack;
	StringBuffer vusbf = null;
	StringBuffer message   = null;

	String vid  = request.getParameter("vid");
	if(vid != null ) {
	    int vid_int = Integer.parseInt(vid);
	    String status =  request.getParameter("stid");
	    String yid = request.getParameter("yid");
	    String bid = request.getParameter("bid");
	    String fid = request.getParameter("fid");

	    if(status == null) {
		status = "0";
	    }
	    
	    int stid = Integer.parseInt(status);
	    if(vid_int > stid || vid_int == 0 || vid_int == 4) {
		if(uBean == null) {
		    uBean = new ScheduleDUpdate();
		}
		vusbf = uBean.main(request, call_path, mid, aid, user_name, user_level);
	    }
	    else {
		HttpSession session = request.getSession(true);
		if(vid_int == 1) {
		    message = printBar(request.getContextPath());
		    vusbf = viewDetailRecords(request, call_path, mid, aid, user_name, user_level, yid, bid, status, fid, vid);
		    session.setAttribute("PrintBuffer", vusbf);
		}

		if(vid_int == 3) {
		    message = printBar(request.getContextPath());
		    vusbf = viewPreYearHoldingRecords(request, call_path, mid, aid, user_name, user_level, yid, bid, status, fid, vid);
		    session.setAttribute("PrintBuffer", vusbf);
		}

		if(vid_int == 5) {
		    message = printBar(request.getContextPath());
		    vusbf = viewThisYearHolding(request, call_path, mid, aid, user_name, user_level, yid, bid, status, 
						fid, vid, manager, admin);
		    session.setAttribute("PrintBuffer", vusbf);
		}

		if(vid_int == 6) {
		    ArrayList alistd = getScheduleDInfo (user_name, yid, "6");
		    ArrayList alista = getScheduleDInfo (user_name, yid, "0");
		    
		    if(alistd != null && alistd.equals(alista) && alistd.size()>1) {
		        String one_form = request.getParameter("fmid");
			if(one_form == null) {
			    message = new StringBuffer();
			    message.append(viewFinalSumInfo(user_name, user_level, yid, bid, fid));
			    message.append("<BR><B>System found you have "+alistd.size());
			    message.append(" Schedule-D Forms for year ["+yid+"]</B><BR>");
			    message.append("<B>You can combine them to a Single schedule-D form.</B><BR><BR>");
			    message.append(MyHtml.makeLinkButton("View Single Schedule-D", call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
								 "&vid=6&yid="+yid+"&bid="+bid+"&stid="+status+"&fmid=1"));
			    message.append("<BR><BR>");
			    vusbf = viewPrintSingleAccountScheduleD(request, user_name, user_level, yid, bid, fid);
			}
			else if(one_form != null && one_form.equals("1")) {
			    vusbf = new StringBuffer ();
			    vusbf.append(viewFinalSumInfo(user_name, user_level, yid));
			    vusbf.append("<BR><H3>This Schedule-D combined your year ["+yid+"] "+alistd.size()+ " Schedule-Ds</H3>");
			    vusbf.append(viewPrintFullYearScheduleD(request, fid, user_name, user_level, yid, alistd));
			}
		    }
		    else {
			vusbf = new StringBuffer ();
			vusbf.append(viewFinalSumInfo(user_name, user_level, yid, bid, fid));
			vusbf.append("<BR>"+printBar(request.getContextPath()));
			vusbf.append("<BR>"+viewPrintSingleAccountScheduleD(request, user_name, user_level, yid, bid, fid));
		    }
		}
	    }
	}
	
	StringBuffer sbf = listAll1099Records(call_path, mid, aid, user_name, user_level, manager, admin);
	if(message != null) {
	    sbf.append("<BR>"+message);
	}
	if(vusbf != null) {
	    sbf.append("<BR>"+vusbf);
	}
	return sbf;
    }

    private StringBuffer printBar(String context_path) {
	StringBuffer sbf = new StringBuffer ();
	sbf.append(MyHtml.makeLinkButton("<B>Print</B>", "'"+context_path+"/Print' target='_blank'"));
	return sbf;
    }

    private StringBuffer listAll1099Records(String call_path, String mid, String aid, String user_name, 
					    int user_level, boolean manager, boolean admin) {
	StringBuffer sbf   = new StringBuffer ();
	ArrayList alist    = null;
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    alist = ejbdb.selectUserScheduleDInfo(user_name);
	}
	catch(Exception e) {
	    System.out.println("listAll1099Records:Error:101:"+e);
	}
	
	sbf.append("<BR><BR><TABLE border=1  cellSpacing=0 cellPadding=2 width=90%>");
	sbf.append("<TR><TD align=center>-</TD>");
	sbf.append("<TD align=center>YEAR</TD>");
	sbf.append("<TD align=center>BROKER</TD>");
	sbf.append("<TD align=center>TOTAL SOLD</TD>");
	sbf.append("<TD align=center>TRANSACTION DATA</TD>");
	sbf.append("<TD align=center>BASE YEAR</TD>");
	sbf.append("<TD align=center>PRE-YEAR HOLDING</TD>");
	sbf.append("<TD align=center>TICK CHANGE</TD>");
	sbf.append("<TD align=center>THIS HOLDING</TD>");
	sbf.append("<TD align=center>STATUS</TD>");
	sbf.append("</TR>");

	Object o = null;
	ArrayList a = null;
	StringBuffer rsbf = new StringBuffer("");

	try {
	    if(alist != null) {
		String fid    = null;
		String bid    = null;
		String yid   = null;
		String t_sold = null;
		String status = null;

		Iterator it1 = alist.iterator();
		Iterator it2 = null;
		int m=0;
		int stid = 0;

		Vector tempv = new Vector();

		while (it1.hasNext()) {
		    a = (ArrayList)it1.next();
		    fid    = (String)a.get(0);
		    bid    = (String)a.get(1);
		    t_sold = (String)a.get(2);
		    yid    = (String)a.get(3);
		    status = (String)a.get(4);

		    if(status == null) {
			status = "0";
		    }

		    stid = Integer.parseInt(status);

		    rsbf.append("<TR>");
		    
		    // column delete 
		    if(stid<lock_level || admin) {
			rsbf.append(MyHtml.makeLinkCell("Delete",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=0&yid="+yid+"&bid="+bid+"&stid="+status));
		    }
		    else {
			rsbf.append("<TD align=center>Locked</TD>");
		    }

		    // year
		    rsbf.append("<TD align=center>"+yid+"</TD>");

		    // broker
		    rsbf.append("<TD align=center>"+getBrokerName("0"+bid)+"</TD>");
		    
		    // total sold
		    rsbf.append("<TD align=right>"+td.format(Double.parseDouble(t_sold))+"</TD>");

		    switch (stid) {
		    case 0:
			rsbf.append(MyHtml.makeLinkCell("View/Confirm",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=1&yid="+yid+"&bid="+bid+"&stid="+status));
			rsbf.append("<TD align=center>N/A</TD>"); //Base year
			rsbf.append("<TD align=center>N/A</TD>"); //pre-holding:Add/View/Confirm
			rsbf.append("<TD align=center>N/A</TD>"); //tick change
			rsbf.append("<TD align=center>N/A</TD>"); //this-holding:View
			rsbf.append("<TD align=center>Confirming Data</TD>"); //Status
			break;

		    case 1:
			//transaction data
			rsbf.append(MyHtml.makeLinkCell("View",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=1&yid="+yid+"&bid="+bid+"&stid="+status));
			//base year
			if(!tempv.contains(bid)) {
			    rsbf.append(MyHtml.makeLinkCell("Confirm",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							    "&vid=2&yid="+yid+"&bid="+bid+"&stid="+status));
			}
			else {
			    rsbf.append("<TD align=center>N/A</TD>");
			}

			//pre-holding
			if(tempv.contains(bid)) {
			    rsbf.append(MyHtml.makeLinkCell("View",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							    "&vid=2&yid="+yid+"&bid="+bid+"&stid="+status));
			}
			else {
			    rsbf.append("<TD align=center>NA</TD>");
			}
			//rsbf.append("<TD align=center>N/A</TD>"); //pre-holding
			rsbf.append("<TD align=center>N/A</TD>"); //tick change
			rsbf.append("<TD align=center>N/A</TD>"); //this-holding:View
			rsbf.append("<TD align=center>"+stid+"</TD>"); //Status
			break;

		    case 2:
			rsbf.append(MyHtml.makeLinkCell("View",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=1&yid="+yid+"&bid="+bid+"&stid="+status));
			if(!tempv.contains(bid)) {
			    rsbf.append(MyHtml.makeLinkCell("Confirm",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							    "&vid=2&yid="+yid+"&bid="+bid+"&stid="+status));
			}
			else {
			    rsbf.append("<TD align=center>"+(Integer.parseInt(yid)-1)+"</TD>");
			}
			
			if(!tempv.contains(bid)) {
			    rsbf.append(MyHtml.makeLinkCell("Add/Confirm",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							    "&vid=3&yid="+yid+"&bid="+bid+"&stid="+status));
			}
			else {
			    rsbf.append(MyHtml.makeLinkCell("View/Confirm",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							    "&vid=3&yid="+yid+"&bid="+bid+"&stid="+status));
			}
			rsbf.append("<TD align=center>N/A</TD>"); //tick change
			rsbf.append("<TD align=center>N/A</TD>"); //this-holding:View
			rsbf.append("<TD align=center>"+stid+"</TD>"); //Status
			break;

		    case 3:
			rsbf.append(MyHtml.makeLinkCell("View",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=1&yid="+yid+"&bid="+bid+"&stid="+status));
			rsbf.append("<TD align=center>"+(Integer.parseInt(yid)-1)+"</TD>");
			rsbf.append(MyHtml.makeLinkCell("View",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=3&yid="+yid+"&bid="+bid+"&stid="+status));

			rsbf.append(MyHtml.makeLinkCell("Update/Confirm",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=4&yid="+yid+"&bid="+bid+"&stid="+status));
			rsbf.append("<TD align=center>N/A</TD>");
			rsbf.append("<TD align=center>"+stid+"</TD>");
			break;
		    case 4:
			rsbf.append(MyHtml.makeLinkCell("View",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=1&yid="+yid+"&bid="+bid+"&stid="+status));
			rsbf.append("<TD align=center>"+(Integer.parseInt(yid)-1)+"</TD>");
			rsbf.append(MyHtml.makeLinkCell("View",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=3&yid="+yid+"&bid="+bid+"&stid="+status));
			
			rsbf.append(MyHtml.makeLinkCell("Update",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=4&yid="+yid+"&bid="+bid+"&stid="+status));
			rsbf.append(MyHtml.makeLinkCell("View/Confirm",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=5&yid="+yid+"&bid="+bid+"&stid="+status));
			rsbf.append("<TD align=center>"+stid+"</TD>");
			break;
		    
		    case 5:
			rsbf.append(MyHtml.makeLinkCell("View",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=1&yid="+yid+"&bid="+bid+"&stid="+status));
			rsbf.append("<TD align=center>"+(Integer.parseInt(yid)-1)+"</TD>");
			rsbf.append(MyHtml.makeLinkCell("View",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=3&yid="+yid+"&bid="+bid+"&stid="+status));
			
			rsbf.append(MyHtml.makeLinkCell("Update",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=4&yid="+yid+"&bid="+bid+"&stid="+status));
			rsbf.append(MyHtml.makeLinkCell("View",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=5&yid="+yid+"&bid="+bid+"&stid="+status));

			rsbf.append(MyHtml.makeLinkCell("Final",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=6&yid="+yid+"&bid="+bid+"&stid="+status));
			break;

		    case 6:
			rsbf.append(MyHtml.makeLinkCell("View",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=1&yid="+yid+"&bid="+bid+"&stid="+status));
			rsbf.append("<TD align=center>"+(Integer.parseInt(yid)-1)+"</TD>");
			rsbf.append(MyHtml.makeLinkCell("View",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=3&yid="+yid+"&bid="+bid+"&stid="+status));
			/*
			rsbf.append(MyHtml.makeLinkCell("Update",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=4&yid="+yid+"&bid="+bid+"&stid="+status));
			*/
			rsbf.append("<TD align=center>Updated</TD>");
			rsbf.append(MyHtml.makeLinkCell("View",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=5&yid="+yid+"&bid="+bid+"&stid="+status));
			rsbf.append(MyHtml.makeLinkCell("ScheduleD",call_path+"?mid="+mid+"&aid="+aid+"&fid="+fid+
							"&vid=6&yid="+yid+"&bid="+bid+"&stid="+status));
			break;
		    }

		    rsbf.append("</TR>");

		    if(!tempv.contains(bid)) {
			tempv.add(bid);
		    }
		}

		tempv = null;
	    }
	    else {
		rsbf.append("<TR><TD colspan=10 align=center>");
		rsbf.append("No record. Please click [Add] to start.</TD></TR>");
	    }
	}
	catch(Exception e) {
	    System.out.println("listAll1099Records:Error:102:rsbf=["+rsbf+"]:Error["+e+"]");
	    rsbf = new StringBuffer("<TR><TD colspan=10 align=center>");
	    rsbf.append("<img src=/images/star.gif>");
	    rsbf.append("Error: loading records. Please try again.</TD></TR>");
	}
	sbf.append(rsbf);
	sbf.append("</TABLE>");
	return sbf;
    }
        
    private StringBuffer viewDetailRecords (HttpServletRequest request, String call_path, String mid, String aid, 
					    String user_name, int user_level, String yid, String bid, String stid, 
					    String fid, String vid) {
	if(vid == null) {
	    return null;
	}

	String record_status = "0"; 
	StringBuffer sbf = new StringBuffer();
	String stick = request.getParameter("stick");
	String sort = request.getParameter("sort");

	int order = 0;
	if(sort != null) {
	    order = Integer.parseInt(sort);
	}

	Vector v = null;
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    //==========================================================================
	    // 0: [order by date] 1: [order by bs] 2: [order by tick]
	    //=========================================================================
	    if(stick == null) {
		v = ejbdb.selectScheduleDDetailRecords(fid, record_status, order);
	    }
	    else {
		v = ejbdb.selectScheduleDDetailRecords(fid, record_status, order, stick);
	    }
	}
	catch (Exception e) {
	    System.out.println("ScheduleDViewer:viewDetailRecord:"+e);
	}

	if(v == null || v.isEmpty()) {
	    return sbf.append("No Pre-Year Holding Data.");
	}

	sbf.append("<TABLE width=90% border=1>");
	sbf.append("<TR><TD align=center colspan=6><B>Transaction Data of YEAR ["+yid+"]");
	sbf.append(" BROKER ["+getBrokerName(bid)+"] Detail Records</TD></TR>");
	sbf.append("</TD></TR>");	

	StringBuffer fsbf = new StringBuffer();
	fsbf.append("<FORM method=post action="+call_path+">");
	fsbf.append("<input type=hidden name=mid value="+mid+">");
	fsbf.append("<input type=hidden name=aid value="+aid+">");
	fsbf.append("<input type=hidden name=fid value="+fid+">");
	fsbf.append("<input type=hidden name=vid value="+vid+">");
	fsbf.append("<input type=hidden name=stid value="+stid+">");
	fsbf.append("<input type=hidden name=bid value="+bid+">");
	fsbf.append("<input type=hidden name=yid value="+yid+">");

	sbf.append("<TR>");
	if(order == 1) {
	    sbf.append("<TD align=center>Bought/Sold</TD>");
	}
	else {
	    sbf.append(fsbf);
	    sbf.append("<TD align=center>Bought/Sold<input type='image' src='/images/sort.gif' border='0'></TD>");
	    sbf.append("<input type=hidden name=sort value=1>");
	    sbf.append("</FORM>");
	}

	sbf.append("<TD align=center>COST</TD>");
	if(order == 2) {
	    sbf.append("<TD align=center>TICK</TD>");
	}
	else {
	    sbf.append(fsbf);
	    sbf.append("<TD align=center>TICK<input type='image' src='/images/sort.gif' border='0'></TD>");
	    sbf.append("<input type=hidden name=sort value=2>");
	    sbf.append("</FORM>");
	}
	sbf.append("<TD align=center>SHARES</TD>");
	sbf.append("<TD align=center>PRICE</TD>");
	
	if(order == 0) {
	    sbf.append("<TD align=center>DATE</TD>");
	}
	else {
	    sbf.append(fsbf);
	    sbf.append("<TD align=center>DATE<input type='image' src='/images/sort.gif' border='0'></TD>");
	    sbf.append("<input type=hidden name=sort value=0>");
	    sbf.append("</FORM>");
	}
	sbf.append("<TR>");

	Enumeration enum = v.elements();
	String bs    = null;
	double cost  = 0.0;
	String tick  = null;
	int share    = 0;
	double price = 0.0;
	String date  = null;
	
	try {
	    while(enum.hasMoreElements()) {
		bs    = (String)enum.nextElement();
		cost  = Double.parseDouble(""+enum.nextElement());
		tick  = (String)enum.nextElement();
		share = (new Double(""+enum.nextElement())).intValue();
		price = Double.parseDouble(""+enum.nextElement());
		date  = (String)enum.nextElement();
		
		sbf.append("<TR>");
		sbf.append("<TD align=center>"+bs+"</TD>");
		sbf.append("<TD align=right>"+cost+"</TD>");
		sbf.append("<TD align=center>"+tick+"</TD>");
		sbf.append("<TD align=right>"+share+"</TD>");
		sbf.append("<TD align=right>"+price+"</TD>");
		sbf.append("<TD align=center>"+date+"</TD>");
		sbf.append("</TR>");
	    }
	}
	catch (Exception e) {
	    System.out.println("viewTransactionDetailRecords:Error:100:"+e);
	}

	sbf.append("</TABLE>");
	enum = null;
	v    = null;
	return sbf;
    }

    private StringBuffer viewPreYearHoldingRecords(HttpServletRequest request, String call_path, 
						   String mid, String aid, String user_name, 
						   int user_level, String yid, String bid, String stid, 
						   String fid, String vid) {
	if(vid == null || !vid.equals("3")) {
	    return null;
	}

	String record_status = "3";
	StringBuffer sbf = new StringBuffer();
	Vector v = null;

	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    if(isBaseYear(user_name, bid, yid)) {
		v = ejbdb.selectScheduleDDetailRecords(fid, record_status, 0); // 0: order by date
	    }
	    else{
		v = ejbdb.getLastYearDetailHoldingRecords(user_name, yid, bid);
	    }
	}
	catch (Exception e) {
	    System.out.println("ScheduleDViewer:viewDetailRecord:"+e);
	}

	if(v == null || v.isEmpty()) {
	    return sbf.append("<H3>No Pre-Year Holding Data.</H3>");
	}

	sbf.append("<TABLE width=90% border=1 cellSpacing=0 cellPadding=2>");
	sbf.append("<TR><TD align=center colspan=6><BR><H3>PRE-YEAR ["+yid+"] HOLDING ");
	sbf.append(" BROKER ["+getBrokerName(bid)+"]</H3><BR></TD></TR>");
	sbf.append("</TD></TR>");	

	sbf.append("<TR>");
	sbf.append("<TD align=center>BOUGHT/SOLD</TD>");
	sbf.append("<TD align=center>COST</TD>");
	sbf.append("<TD align=center>TICK</TD>");
	sbf.append("<TD align=center>SHARES</TD>");
	sbf.append("<TD align=center>PRICE</TD>");
	sbf.append("<TD align=center>DATE</TD>");
	sbf.append("<TR>");

	Enumeration enum = v.elements();
	String bs    = null;
	double cost  = 0.0;
	String tick  = null;
	int share    = 0;
	double price = 0.0;
	String date  = null;
	
	try {
	    while(enum.hasMoreElements()) {
		bs    = (String)enum.nextElement();
		cost  = Double.parseDouble(""+enum.nextElement());
		tick  = (String)enum.nextElement();
		share = (new Double(""+enum.nextElement())).intValue();
		price = Double.parseDouble(""+enum.nextElement());
		date  = (String)enum.nextElement();
		
		sbf.append("<TR>");
		sbf.append("<TD align=center>"+bs+"</TD>");
		sbf.append("<TD align=right>"+td.format(java.lang.Math.abs(cost))+"</TD>");
		sbf.append("<TD align=center>"+tick+"</TD>");
		sbf.append("<TD align=right>"+share+"</TD>");
		sbf.append("<TD align=right>"+td.format(price)+"</TD>");
		sbf.append("<TD align=center>"+date+"</TD>");
		sbf.append("</TR>");
	    }
	}
	catch (Exception e) {
	    System.out.println("viewTransactionDetailRecords:Error:100:"+e);
	}

	sbf.append("</TABLE>");
	enum = null;
	v    = null;
	return sbf;
    }

    private StringBuffer viewTickChagen (HttpServletRequest request, String call_path, String mid, String aid, 
					 String user_name,  int user_level, String yid, String bid, String stid, 
					 String fid, String vid) {
	StringBuffer sbf = null;
	try {
	    return sbf;
	}
	catch (Exception e) {
	    System.out.println("ScheduleDViewer:viewTickChange:Error:"+e);
	}
	return null;
    }

    private StringBuffer viewThisYearHolding (HttpServletRequest request, String call_path, String mid, String aid, 
					      String user_name,  int user_level, String yid, String bid, String stid, 
					      String fid, String vid, boolean manager, boolean admin) {
	StringBuffer sbf = null;
	try {
	    sbf = new StringBuffer();
	    sbf.append("<TABLE width=90% border=1 cellSpacing=0 cellPadding=2>");
	    sbf.append("<TR><TD align=center colspan=6>");
	    sbf.append("<BR><H3>YEAR ["+yid+"] BROKER ["+getBrokerName(bid)+"] HOLDING RECORDS</H3>");
	    sbf.append("</TD>");
	    sbf.append("</TR>");
	    sbf.append("<TR><TD align=center>BOUGHT/SOLD</TD><TD align=center>COST</TD>");
	    sbf.append("<TD align=center>TICK</TD><TD align=center>SHARES</TD>");
	    sbf.append("<TD align=center>PRICE</TD><TD align=center>DATE</TD></TR>");
	    
	    Vector vr = ejbdb.selectHoldingRecords(fid, user_name, yid, bid); 
	    if(vr != null) {
		StockRecord r = null;
		Enumeration enum = vr.elements();
		while(enum.hasMoreElements()) {
		    r = (StockRecord)enum.nextElement();
		    sbf.append("<TR>");
		    sbf.append("<TD align=center>"+r.getBuySell()+"</TD>");
		    sbf.append("<TD align=right>"+td.format(java.lang.Math.abs(r.getCost()))+"</TD>");
		    sbf.append("<TD align=center>"+r.getTick()+"</TD>");
		    sbf.append("<TD align=right>"+r.getShare()+"</TD>");
		    sbf.append("<TD align=right>"+td.format(r.getPrice())+"</TD>");
		    sbf.append("<TD align=center>"+r.getDate()+"</TD>");
		    sbf.append("</TR>");
		}
	    }

	    sbf.append("</TABLE>");

	    return sbf;
	}
	catch (Exception e) {
	    System.out.println("ScheduleDViewer:viewThisYearHolding:Error:"+e);
	}
	return null;
    }

    private ArrayList getScheduleDInfo(String user_name, String year, String status) {
	//==========================================================================
	// return this user's Form-1099's 
	// ArrayList[ArrayList(ID, BROKER_ID, TOTAL_SOLD)] WHERE STATUS >= status
	//==========================================================================
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    return ejbdb.getScheduleDInfo(user_name, year, status);
	}
	catch (Exception e) {
	    System.out.println("ScheduleDViewer:getScheduleDInfo:Error:"+e);
	}
	return null;
    }

    private StringBuffer viewPrintFullYearScheduleD(HttpServletRequest request, String fid, String user_name, int user_level,  
						    String yid, ArrayList alist) {
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    Iterator it = alist.iterator();
	    String id     = null;
	    String bid    = null;
	    String t_sold = null;
	    ArrayList a = null;
	    Vector v = null;
	    Vector vr = new Vector();
	    while(it.hasNext()) {
		a      = (ArrayList)it.next();
		id     = (String)a.get(0);
		bid    = (String)a.get(1);
		t_sold = (String)a.get(2);
		v = ejbdb.getFinalRecords(id, user_name, yid, bid);
		vr = addElements(vr, v);
	    }

	    if(vr == null) {
		return (new StringBuffer("<img src=/images/star.gif>Loading Data Error. Please try again."));
	    }
	    else {
		return printScheduleD(request, vr, user_name, user_level, yid, null, null);
	    }
	}
	catch (Exception e) {
	    System.out.println("ScheduleDViewer:viewPrintFullYearScheduleD:Error:"+e);
	}
	return null;
    }

    private StringBuffer viewPrintSingleAccountScheduleD (HttpServletRequest request, String user_name, int user_level, 
					     String yid, String bid, String fid) {
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    Vector vr = ejbdb.getFinalRecords(fid, user_name, yid, bid);

	    if(vr == null) {
		return (new StringBuffer("<img src=/images/star.gif>Loading Data Error. Please try again."));
	    }
	    else {
		return printScheduleD(request, vr, user_name, user_level, yid, bid, fid);
	    }
	}
	catch (Exception e) {
	    System.out.println("ScheduleDViewer:viewPrintSingleAccountScheduleD:Error:"+e);
	}
	return null;
    }

    private StringBuffer printScheduleD2003 (HttpServletRequest request, Vector vr, String user_name, int user_level, 
					     String yid, String bid, String fid) {
	int set_date = 20030505;
	StringBuffer sbf = new StringBuffer();
	
	try {
	    Vector v  = null;

	    String share  = null;
	    String tick   = null;

	    String date_b = null;
	    String date_s = null;
	 
	    String cost_s = null;
	    String cost_b = null;

	    String gain   = null;

	    double total_s = 0.0;
	    double total_b = 0.0;
	    double total_g = 0.0;

	    double v_total_s = 0.0; //Verify the results
	    double v_total_b = 0.0; //Verify the results
	    double v_total_g = 0.0; //Verify the results

	    double page1_b = 0.0;
	    double page1_s = 0.0;
	    double page1_g = 0.0;

	    double page_b  = 0.0;
	    double page_s  = 0.0;
	    double page_g  = 0.0;

	    double m_total_s = 0.0;
	    double m_total_b = 0.0;
	    double m_total_g = 0.0;

	    double m_page1_b = 0.0;
	    double m_page1_s = 0.0;
	    double m_page1_g = 0.0;

	    double m_page_b  = 0.0;
	    double m_page_s  = 0.0;
	    double m_page_g  = 0.0;

	    StringBuffer p1sbf = new StringBuffer();
	    StringBuffer psbf = new StringBuffer();
	    p1sbf.append("<HTML><HEAD></HEAD><BODY>");
	    p1sbf.append("\n<TABLE width=660 border=0 bordercolor=#FFFFFF cellSpacing=0 cellPadding=0 style='font-size: 10pt'>");
	    p1sbf.append("<TR><TD height=135 colspan=8>&nbsp</TD></TR>");

	    int n = 0;
	    int m = 0;
	    Enumeration enumr = null;
	    Enumeration enum  = vr.elements();
	    while(enum.hasMoreElements()) {
		n++;
		m++;
		v = (Vector)enum.nextElement();
		enumr = v.elements();
		while(enumr.hasMoreElements()) {
		    share  = (String)enumr.nextElement();
		    tick   = (String)enumr.nextElement();
		    date_b = (String)enumr.nextElement();
		    date_s = (String)enumr.nextElement();
		    cost_s = (String)enumr.nextElement();
		    cost_b = (String)enumr.nextElement();
		    gain   = (String)enumr.nextElement();
		}

		if(n<=4){
		    page1_b += java.lang.Math.abs(Double.parseDouble(cost_b));
		    page1_s += java.lang.Math.abs(Double.parseDouble(cost_s));
		    page1_g += Double.parseDouble(gain);

		    p1sbf.append("<TR>");
		    p1sbf.append("<TD height=31 width=60 align=right style='font-size: 10pt'>"+share+"</TD>");
		    p1sbf.append("<TD width=60 align=center style='font-size: 10pt'>"+tick+"</TD>");
		    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+date_b+"</TD>");
		    p1sbf.append("<TD width=90 align=center style='font-size: 10pt'>"+date_s+"</TD>");
		    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+cost_s+"</TD>");
		    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+cost_b+"</TD>");
		    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+gain+"</TD>");

		    if(afterDate(date_s, set_date)) {
			p1sbf.append("<TD width=90 align=right>"+gain+"</TD>");
			m_page1_b += java.lang.Math.abs(Double.parseDouble(cost_b));
			m_page1_s += java.lang.Math.abs(Double.parseDouble(cost_s));
			m_page1_g += Double.parseDouble(gain);
		    }
		    else {
			p1sbf.append("<TD width=90>&nbsp</TD>");
		    }
		    p1sbf.append("</TR>");
		}

		if(m>4){
		    if(m==5){
			psbf.append("\n<div style='page-break-before:always'>");
			psbf.append("\n<TABLE width=660 border=0 bordercolor=#FFFFFF cellSpacing=0 cellPadding=0>");
			psbf.append("<TR><TD height=135 colspan=8>&nbsp</TD></TR>");
		    }

		    if(m != 5 && (m-5)%24==0){
			psbf.append("<TR>");
			psbf.append("<TD height=31 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
			psbf.append("<TD width=60 align=center style='font-size: 10pt'>&nbsp;</TD>");
			psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
			psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
			psbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(page_s)+"</TD>");
			psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
			psbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(page_g)+"</TD>");
			if(m_page_g != 0.0) {
			    psbf.append("<TD width=90 align=right>"+td.format(m_page_g)+"</TD>");
			}
			else {
			    psbf.append("<TD width=90>&nbsp;</TD>");
			}
			psbf.append("</TR>");

			total_b += java.lang.Math.abs(page_b);
			total_s += java.lang.Math.abs(page_s);
			total_g += page_g;

			m_total_b += java.lang.Math.abs(m_page_b);
			m_total_s += java.lang.Math.abs(m_page_s);
			m_total_g += m_page_g;

			page_b = 0.0;
			page_s = 0.0;
			page_g = 0.0;

			m_page_b = 0.0;
			m_page_s = 0.0;
			m_page_g = 0.0;

			psbf.append("\n</TABLE>");
			psbf.append("\n</div>");
			psbf.append("\n<div style='page-break-before:always'>");
			psbf.append("\n<TABLE width=660 border=0 bordercolor=#FFFFFF cellSpacing=0 cellPadding=0>");
			psbf.append("<TR><TD height=135 colspan=8>&nbsp</TD></TR>");
		    }

		    psbf.append("<TR>");
		    psbf.append("<TD height=31 width=60 align=right style='font-size: 10pt'>"+share+"</TD>");
		    psbf.append("<TD width=60 align=center style='font-size:10pt'>"+tick+"</TD>");
		    psbf.append("<TD width=90 align=right style='font-size:10pt'>"+date_b+"</TD>");
		    psbf.append("<TD width=90 align=right style='font-size:10pt'>"+date_s+"</TD>");
		    psbf.append("<TD width=90 align=right style='font-size:10pt'>"+java.lang.Math.abs(Double.parseDouble(cost_s))+"</TD>");
		    psbf.append("<TD width=90 align=right style='font-size:10pt'>"+java.lang.Math.abs(Double.parseDouble(cost_b))+"</TD>");
		    psbf.append("<TD width=90 align=right style='font-size:10pt'>"+gain+"</TD>");

		    if(afterDate(date_s, set_date)) {
			psbf.append("<TD width=90 align=right>"+gain+"</TD>");
			m_page_b += java.lang.Math.abs(Double.parseDouble(cost_b));
			m_page_s += java.lang.Math.abs(Double.parseDouble(cost_s));
			m_page_g += Double.parseDouble(gain);
		    }
		    else {
			psbf.append("<TD width=90>&nbsp;</TD>");
		    }
		    psbf.append("</TR>");

		    page_b += java.lang.Math.abs(Double.parseDouble(cost_b));
		    page_s += java.lang.Math.abs(Double.parseDouble(cost_s));
		    page_g += Double.parseDouble(gain);
		}
	    }

	    //Line 2
	    if(m>4) {
		// total_* and m_total_* may or may not include last page total
		// depands on how many rows. if last page included, the last page
		// must be set to 0.00
		// so just simply added the last page.

		total_b += java.lang.Math.abs(page_b);
		total_s += java.lang.Math.abs(page_s);
		total_g += page_g;
		
		m_total_b += java.lang.Math.abs(m_page_b);
		m_total_s += java.lang.Math.abs(m_page_s);
		m_total_g += m_page_g;

		p1sbf.append("<TR>");
		p1sbf.append("<TD height=31 width=60 align=right style='font-size: 10pt'></TD>");
		p1sbf.append("<TD width=60 align=center style='font-size: 10pt'></TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'></TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'></TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(total_s)+"</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'></TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(total_g)+"</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(m_total_g)+"</TD>");
		p1sbf.append("</TR>");
	    }
	    else {
		p1sbf.append("<TR>");
		p1sbf.append("<TD height=31 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=60 align=center style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90>&nbsp</TD>");
		p1sbf.append("</TR>");
	    }

	    //Line 3
	    p1sbf.append("<TR>");
	    p1sbf.append("<TD height=31 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=50 align=center style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(total_s+page1_s)+"</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90>&nbsp</TD>");
	    p1sbf.append("</TR>");
	    //Line 4
	    p1sbf.append("<TR>");
	    p1sbf.append("<TD height=31 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=60 align=center style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90>&nbsp</TD>");
	    p1sbf.append("</TR>");
	    //Line 5
	    p1sbf.append("<TR>");
	    p1sbf.append("<TD height=31 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=60 align=center style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90>&nbsp</TD>");
	    p1sbf.append("</TR>");
	    
	    //Line 6
	    double carr = getCarryOver(user_name, user_level, yid, bid, fid);
	    p1sbf.append("<TR>");
	    p1sbf.append("<TD height=31 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=60 align=center style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(carr)+"</TD>");
	    p1sbf.append("<TD width=90>&nbsp</TD>");
	    p1sbf.append("</TR>");

	    //Line 7a
	    p1sbf.append("<TR>");
	    p1sbf.append("<TD height=31 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=60 align=center style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");

	    if((m_total_g+m_page1_g)<0) {
		p1sbf.append("<TD width=90 align=right>"+td.format(m_total_g+m_page1_g)+"</TD>");
	    }
	    else {
		p1sbf.append("<TD width=90 align=right align=right style='font-size: 10pt'>0.00</TD>");
	    }
	    p1sbf.append("</TR>");

	    //Line 7b
	    p1sbf.append("<TR>");
	    p1sbf.append("<TD height=31 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=60 align=center style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right valign=top style='font-size: 10pt'>"+td.format(total_g+page1_g-carr)+"</TD>");
	    p1sbf.append("<TD width=90>&nbsp</TD>");
	    p1sbf.append("</TR>");
	    
	    for(int line = 11; line <24; line++) {
		p1sbf.append("<TR>");
		p1sbf.append("<TD height=31 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=60 align=center style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90>&nbsp</TD>");
		p1sbf.append("</TR>");
	    }

	    p1sbf.append("\n</TABLE>");

	    if((m-5)%24 !=0 && m > 5){
		int end  = (m-5)%24;
		int more = 24-end;
		for(int j= end+1; j<24; j++) {
		    psbf.append("<TR>");
		    psbf.append("<TD height=31 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
		    psbf.append("<TD width=60 align=center style='font-size: 10pt'>&nbsp;</TD>");
		    psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		    psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		    psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		    psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		    psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		    psbf.append("<TD width=90>&nbsp;</TD>");
		    psbf.append("</TR>");
		}
		psbf.append("<TR>");
		psbf.append("<TD height=31 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
		psbf.append("<TD width=60 align=center style='font-size: 10pt'>&nbsp;</TD>");
		psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		psbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(page_s)+"</TD>");
		psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		psbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(page_g)+"</TD>");
		if(m_page_g != 0.0) {
		    psbf.append("<TD width=90 align=right>"+td.format(m_page_g)+"</TD>");
		}
		else {
		    psbf.append("<TD width=90>&nbsp;</TD>");
		}
		psbf.append("</TR>");
		psbf.append("\n</TABLE>");
		psbf.append("\n</div>");
	    }
	    psbf.append("</BODY></HTML>");

	    HttpSession session = request.getSession(true);
	    p1sbf.append(psbf);
	    session.setAttribute("PrintBuffer", p1sbf);
	    
	    sbf.append("<TABLE width=90% border=1 cellSpacing=0 cellPadding=0>");
	    sbf.append("<TR><TD align=center>");
	    sbf.append(p1sbf);
	    sbf.append("</TD></TR>");
	    sbf.append("</TABLE>");
	    return sbf;
	}
	catch (Exception e) {
	    System.out.println("ScheduleDViewer:printScheduleD:Error:"+e);
	}
	return null;
    }

    private boolean afterDate(String date, int set_date) {
	if(Integer.parseInt(date.replaceAll("-",""))>set_date) {
	    return true;
	}
	return false;
    }

    private StringBuffer printScheduleD (HttpServletRequest request, Vector vr, String user_name, int user_level, 
					 String yid, String bid, String fid) {
	if(yid.equals("2003")) {
	    return printScheduleD2003(request, vr, user_name, user_level, yid, bid, fid);
	}

	StringBuffer sbf = new StringBuffer();
	
	try {
	    Vector v  = null;

	    String share  = null;
	    String tick   = null;

	    String date_b = null;
	    String date_s = null;
	 
	    String cost_s = null;
	    String cost_b = null;

	    String gain   = null;

	    double total_s = 0.0;
	    double total_b = 0.0;
	    double total_g = 0.0;

	    double page1_b = 0.0;
	    double page1_s = 0.0;
	    double page1_g = 0.0;

	    double page_b  = 0.0;
	    double page_s  = 0.0;
	    double page_g  = 0.0;

	    StringBuffer p1sbf = new StringBuffer();
	    StringBuffer psbf = new StringBuffer();

	    p1sbf.append("<TABLE width=660 border=0 cellSpacing=0 cellPadding=0 style='font-size: 10pt'>");
	    p1sbf.append("<TR><TD height=135 colspan=8>&nbsp</TD></TR>");

	    int n = 0;
	    int m = 0;
	    Enumeration enumr = null;
	    Enumeration enum  = vr.elements();
	    while(enum.hasMoreElements()) {
		n++;
		m++;
		v = (Vector)enum.nextElement();
		enumr = v.elements();
		while(enumr.hasMoreElements()) {
		    share  = (String)enumr.nextElement();
		    tick   = (String)enumr.nextElement();
		    date_b = (String)enumr.nextElement();
		    date_s = (String)enumr.nextElement();
		    cost_s = (String)enumr.nextElement();
		    cost_b = (String)enumr.nextElement();
		    gain   = (String)enumr.nextElement();
		}

		if(n<=4){
		    page1_b += java.lang.Math.abs(Double.parseDouble(cost_b));
		    page1_s += java.lang.Math.abs(Double.parseDouble(cost_s));
		    page1_g += Double.parseDouble(gain);
		    p1sbf.append("<TR>");
		    p1sbf.append("<TD height=29 width=60 align=right style='font-size: 10pt'>"+share+"</TD>");
		    p1sbf.append("<TD width=50 align=center style='font-size: 10pt'>"+tick+"</TD>");
		    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+date_b+"</TD>");
		    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+date_s+"</TD>");
		    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+cost_s+"</TD>");
		    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+cost_b+"</TD>");
		    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+gain+"</TD>");
		    p1sbf.append("<TD width=100>&nbsp</TD>");
		    p1sbf.append("</TR>");
		}

		if(m>4){
		    if(m==5){
			psbf.append("<div style='page-break-before:always'>");
			psbf.append("<TABLE width=660 border=0 cellSpacing=0 cellPadding=0 style='font-size: 10pt'>");
			psbf.append("<TR><TD height=135 colspan=8>&nbsp</TD></TR>");
		    }

		    if(m != 5 && (m-5)%24==0){
			psbf.append("<TR>");
			psbf.append("<TD height=29 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
			psbf.append("<TD width=50 align=center style='font-size: 10pt'>&nbsp;</TD>");
			psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
			psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
			psbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(page_s)+"</TD>");
			psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
			psbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(page_g)+"</TD>");
			psbf.append("<TD width=100>&nbsp;</TD>");
			psbf.append("</TR>");
			page_b = 0.0;
			page_s = 0.0;
			page_g = 0.0;
			psbf.append("</TABLE>");
			psbf.append("</div>");
			psbf.append("<div style='page-break-before:always'>");
			psbf.append("<TABLE width=660 border=0  style='font-size: 10pt' cellSpacing=0 cellPadding=0>");
			psbf.append("<TR><TD height=135 colspan=8>&nbsp</TD></TR>");
		    }

		    psbf.append("<TR>");
		    psbf.append("<TD height=29 width=60 align=right style='font-size: 10pt'>"+share+"</TD>");
		    psbf.append("<TD width=50 align=center style='font-size:10pt'>"+tick+"</TD>");
		    psbf.append("<TD width=90 align=right style='font-size:10pt'>"+date_b+"</TD>");
		    psbf.append("<TD width=90 align=right style='font-size:10pt'>"+date_s+"</TD>");
		    psbf.append("<TD width=90 align=right style='font-size:10pt'>"+java.lang.Math.abs(Double.parseDouble(cost_s))+"</TD>");
		    psbf.append("<TD width=90 align=right style='font-size:10pt'>"+java.lang.Math.abs(Double.parseDouble(cost_b))+"</TD>");
		    psbf.append("<TD width=90 align=right style='font-size:10pt'>"+gain+"</TD>");
		    psbf.append("<TD width=100>&nbsp;</TD>");
		    psbf.append("</TR>");

		    page_b += java.lang.Math.abs(Double.parseDouble(cost_b));
		    page_s += java.lang.Math.abs(Double.parseDouble(cost_s));
		    page_g += Double.parseDouble(gain);
		}

		total_b += java.lang.Math.abs(Double.parseDouble(cost_b));
		total_s += java.lang.Math.abs(Double.parseDouble(cost_s));
		total_g += Double.parseDouble(gain);
	    }

	    if(m>4) {
		p1sbf.append("<TR>");
		p1sbf.append("<TD height=29 width=60 align=right style='font-size: 10pt'></TD>");
		p1sbf.append("<TD width=50 align=center style='font-size: 10pt'></TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'></TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'></TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(total_s-page1_s)+"</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'></TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(total_g-page1_g)+"</TD>");
		p1sbf.append("<TD width=100>&nbsp</TD>");
		p1sbf.append("</TR>");
	    }
	    else {
		p1sbf.append("<TR>");
		p1sbf.append("<TD height=29 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=50 align=center style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=100>&nbsp</TD>");
		p1sbf.append("</TR>");
	    }

	    p1sbf.append("<TR>");
	    p1sbf.append("<TD height=29 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=50 align=center style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(total_s)+"</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=100>&nbsp</TD>");
	    p1sbf.append("</TR>");

	    p1sbf.append("<TR>");
	    p1sbf.append("<TD height=29 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=50 align=center style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=100>&nbsp</TD>");
	    p1sbf.append("</TR>");
		
	    p1sbf.append("<TR>");
	    p1sbf.append("<TD height=29 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=50 align=center style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=100>&nbsp</TD>");
	    p1sbf.append("</TR>");
		
	    double carr = getCarryOver(user_name, user_level, yid, bid, fid);
	   
	    p1sbf.append("<TR>");
	    p1sbf.append("<TD height=29 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=50 align=center style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(carr)+"</TD>");
	    p1sbf.append("<TD width=100>&nbsp</TD>");
	    p1sbf.append("</TR>");

	    p1sbf.append("<TR>");
	    p1sbf.append("<TD height=29 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=50 align=center style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
	    p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(total_g-carr)+"</TD>");
	    p1sbf.append("<TD width=100>&nbsp</TD>");
	    p1sbf.append("</TR>");

	    for(int line = 11; line <=25; line++) {
		p1sbf.append("<TR>");
		p1sbf.append("<TD height=29 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=50 align=center style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		p1sbf.append("<TD width=100>&nbsp</TD>");
		p1sbf.append("</TR>");
	    }

	    p1sbf.append("</TABLE>");
	    p1sbf.append("</div>");

	    if((m-5)%24 !=0 && m > 5){
		int end  = (m-5)%24;
		int more = 24-end;
		for(int j= end+1; j<24; j++) {
		    psbf.append("<TR>");
		    psbf.append("<TD height=29 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
		    psbf.append("<TD width=50 align=center style='font-size: 10pt'>&nbsp;</TD>");
		    psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		    psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		    psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		    psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		    psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		    psbf.append("<TD width=100>&nbsp;</TD>");
		    psbf.append("</TR>");
		}
		psbf.append("<TR>");
		psbf.append("<TD height=29 width=60 align=right style='font-size: 10pt'>&nbsp;</TD>");
		psbf.append("<TD width=50 align=center style='font-size: 10pt'>&nbsp;</TD>");
		psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		psbf.append("<TD width=90 align=right style='font-size: 10pt'>&nbsp;</TD>");
		psbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(page_s)+"</TD>");
		psbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(page_b)+"</TD>");
		psbf.append("<TD width=90 align=right style='font-size: 10pt'>"+td.format(page_g)+"</TD>");
		psbf.append("<TD width=100>&nbsp</TD>");
		psbf.append("</TR>");
		psbf.append("</TABLE>");
		psbf.append("</div>");
	    }
	    
	    HttpSession session = request.getSession(true);
	    p1sbf.append(psbf);
	    session.setAttribute("PrintBuffer", p1sbf);
	    
	    sbf.append("<TABLE width=90% border=1 cellSpacing=0 cellPadding=0>");
	    sbf.append("<TR><TD align=center>");
	    sbf.append(p1sbf);
	    sbf.append("</TD></TR>");
	    sbf.append("</TABLE>");
	    return sbf;
	}
	catch (Exception e) {
	    System.out.println("ScheduleDViewer:printScheduleD:Error:"+e);
	}
	return null;
    }

    private StringBuffer viewFinalSumInfo (String user_name, int user_level, String yid) {
	return viewFinalSumInfo(user_name, user_level, yid, null, null);
    }
    
    private StringBuffer viewFinalSumInfo (String user_name, int user_level, String yid, String bid, String fid) {
	StringBuffer sbf = new StringBuffer();
	
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    String query = null;
	    ArrayList aa = null;
	    ArrayList a  = null;
	    
	    if(bid != null && fid != null){
		query = "SELECT * FROM TAX_SCHEDULE_D_SUM WHERE USER_NAME='"+user_name+"'"+
		    " AND YEAR="+yid+" AND BROKER_ID="+bid+" AND FID="+fid+";";
	    }
	    else {
		query = "SELECT * FROM TAX_SCHEDULE_D_SUM WHERE USER_NAME='"+user_name+"'"+
		    " AND YEAR="+yid+";";
	    }

	    aa = ejbdb.select(query);

	    sbf.append("<TABLE width=90% cellSpacing='0' cellPadding=2 border=1>");
	    sbf.append("<TR>");
	    sbf.append("<TD align=center><B>YEAR</B></TD>");
	    sbf.append("<TD align=center><B>BROKER</B></TD>");
	    sbf.append("<TD align=center><B>INVESTMENT</B></TD>");
	    sbf.append("<TD align=center><B>GAIN(THIS YEAR)</B></TD>");
	    sbf.append("<TD align=center><B>DEDUCTION</B></TD>");
	    sbf.append("<TD align=center><B>LOSS(CARRIEY OVER)</B></TD>");
	    sbf.append("</TR>");

	    String init_value = null;
	    String gain_loss  = null;
	    String deduction  = null;
	    String carry_over = null;
	    String broker_id  = null;
	  
	    double init = 0.0;
	    double gain = 0.0;
	    double dedc = 0.0;
	    double carr = 0.0;

	    // FID(*) USER_NAME YEAR BROKER_ID INIT_VALUE GAIN_LOSS BALANCE DEDUCTION CARRY_OVER 
	    
	    Iterator it = aa.iterator();
	    while(it.hasNext()) {
		a = (ArrayList)it.next();
		broker_id  = (String)a.get(3);
		init_value = (String)a.get(4);
		gain_loss  = (String)a.get(5);
		deduction  = (String)a.get(7);
		carry_over = (String)a.get(8);

		sbf.append("<TR>");
		sbf.append("<TD align=center>"+yid+"</TD>");
		sbf.append("<TD align=center>"+getBrokerName(broker_id)+"</TD>");
		sbf.append("<TD align=right>"+init_value+"</TD>");
		sbf.append("<TD align=right>"+gain_loss+"</TD>");
		sbf.append("<TD align=right>"+deduction+"</TD>");
		sbf.append("<TD align=right>"+td.format(Double.parseDouble(carry_over))+"</TD>");
		sbf.append("</TR>");

		init += Double.parseDouble(init_value);
		gain += Double.parseDouble(gain_loss);
		dedc += Double.parseDouble(deduction);
		carr += Double.parseDouble(carry_over);
	    }

	    if(aa.size()>1) {
		sbf.append("<TR>");
		sbf.append("<TD align=center colspan=2>Total</TD>");
		sbf.append("<TD align=right>"+td.format(init)+"</TD>");
		sbf.append("<TD align=right>"+td.format(gain)+"</TD>");
		sbf.append("<TD align=right>"+td.format(dedc)+"</TD>");
		sbf.append("<TD align=right>"+td.format(carr)+"</TD>");
		sbf.append("</TR>");
	    }
	    sbf.append("</TABLE>");
	}
	catch (Exception e) {
	    System.out.println("ScheduleD:viewFinalSumInfo:"+e);
	}
	return sbf;
    }

    private double getCarryOver (String user_name, int user_level, String yid, String bid, String fid) {
	String carry_over = null;
	String broker_id  = null;
	String gain_loss  = null;
	String deduction  = null;

	double gain = 0.0;
	double carr = 0.0;
	double dedc = 0.0;
	double init_carr  = 0.0;
	double total_carr = 0.0;

	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    
	    String query = null;
	    ArrayList aa = null;
	    ArrayList a  = null;

	    if(bid != null && fid != null){
		query = "SELECT BROKER_ID, GAIN_LOSS, DEDUCTION, CARRY_OVER FROM TAX_SCHEDULE_D_SUM WHERE USER_NAME='"+user_name+"'"+
		    " AND YEAR="+(Integer.parseInt(yid)-1)+" AND BROKER_ID="+bid+";";
	    }
	    else {
		query = "SELECT BROKER_ID, GAIN_LOSS, DEDUCTION, CARRY_OVER FROM TAX_SCHEDULE_D_SUM WHERE USER_NAME='"+user_name+"'"+
		    " AND YEAR="+(Integer.parseInt(yid)-1)+";";
	    }
	    System.out.println(query);
	    aa = ejbdb.select(query);
	    System.out.println(aa);
	    Iterator it = aa.iterator();
	    
	    while(it.hasNext()) {
		a = (ArrayList)it.next();
		broker_id  = (String)a.get(0);
		gain_loss  = (String)a.get(1);
		deduction  = (String)a.get(2);
		carry_over = (String)a.get(3);

		gain = Double.parseDouble(gain_loss);
		carr = Double.parseDouble(carry_over);
		System.out.println("AAAAAAAAAAAA="+carr);
		if(isBaseYear(user_name, broker_id, yid)) {
		    if(gain>0) {
			init_carr = java.lang.Math.abs(carr)+Double.parseDouble(deduction)-gain;
		    }
		    else {
			init_carr = java.lang.Math.abs(carr)+Double.parseDouble(deduction)+gain;
		    }
		}
		else {
		    init_carr = java.lang.Math.abs(carr);
		}
		System.out.println("AAAAAAAAAAAA="+init_carr);
		total_carr += init_carr;
	    }
	}
	catch (Exception e) {
	    System.out.println("ScheduleD:getCarryOver:"+e);
	}

	return total_carr;
    }
}
